#include <iostream>

#include <curl/curl.h>

#include "drive.h"

#define ROVIO_HTTP "http://143.215.111.166"

int main(int argc, char **argv) {
    if (argc < 2) {
        std::cout << "Usage: " << argv[0] << " shape" << std::endl;
        std::cout << "    Where shape = 0 (for square), 1 (for infinity), 2 (for Cha Cha Slide)"
            << std::endl;
        return 1;
    }

    CURL *curl = curl_easy_init();

    switch (atoi(argv[1])) {
        case 0: { // Square
            // Default to 4 legs
            int legs = 4;

            // Third argument can override this
            if (argc == 3) {
                legs = atoi(argv[2]);
            }

            // Default to executing a turn at the end
            bool execLastTurn = true;

            // Fourth argument can override this
            if (argc == 4) {
                execLastTurn = atoi(argv[3]);
            }

            for (int i = 0; i < legs; i++) {
                drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 3500);

                if (i < legs - 1 || execLastTurn) {
                    drive(curl, ROVIO_HTTP, DIR_ROT_RIGHT, SPEED_MEDIUM, 500);
                }
            }

            break;
        }
        case 1: // Infinity
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 2000);
            drive(curl, ROVIO_HTTP, DIR_ROT_LEFT, SPEED_MEDIUM, 750);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 4000);
            drive(curl, ROVIO_HTTP, DIR_ROT_RIGHT, SPEED_MEDIUM, 750);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 2000);
            drive(curl, ROVIO_HTTP, DIR_ROT_RIGHT, SPEED_MEDIUM, 750);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 2000);
            drive(curl, ROVIO_HTTP, DIR_ROT_RIGHT, SPEED_MEDIUM, 750);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 4000);
            drive(curl, ROVIO_HTTP, DIR_ROT_LEFT, SPEED_MEDIUM, 750);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_MEDIUM, 2000);

            break;

        case 2: // Cha Cha Slide
            drive(curl, ROVIO_HTTP, DIR_LEFT, SPEED_MEDIUM, 500);
            drive(curl, ROVIO_HTTP, DIR_BACKWARD, SPEED_MEDIUM, 500);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_FASTEST, 150, 2000);
            drive(curl, ROVIO_HTTP, DIR_ROT_LEFT, SPEED_MEDIUM, 50, 2000);
            drive(curl, ROVIO_HTTP, DIR_ROT_RIGHT, SPEED_MEDIUM, 50, 2000);
            drive(curl, ROVIO_HTTP, DIR_LEFT, SPEED_SLOWEST, 800, 0);
            drive(curl, ROVIO_HTTP, DIR_BACKWARD, SPEED_SLOWEST, 800, 0);
            drive(curl, ROVIO_HTTP, DIR_RIGHT, SPEED_SLOWEST, 800, 0);
            drive(curl, ROVIO_HTTP, DIR_FORWARD, SPEED_SLOWEST, 800, 1000);
            drive(curl, ROVIO_HTTP, DIR_ROT_LEFT, SPEED_MEDIUM, 500);
    }

    return 0;
}
